package com.zyp.test.test;

import android.animation.PropertyValuesHolder;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

/**
 * @author zyp
 *         2017/12/29
 */

public class BezierTestView extends View {

    private Paint mPaint;
    private Path mPath;
    private Point mStartPoint;
    private Point mControlPoint;
    private Point mEndPoint;

    public BezierTestView(Context context) {
        super(context);
        init();
    }

    public BezierTestView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public BezierTestView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }
    private void init(){
        mPaint = new Paint();
        mPaint.setColor(Color.RED);
        mPaint.setStrokeWidth(10);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setAntiAlias(true);
        mPath = new Path();
        mStartPoint = new Point(50,50);
        mEndPoint = new Point(333,88);
        mControlPoint = new Point((mEndPoint.x - mStartPoint.x)/2+ mStartPoint.x,(mEndPoint.y - mStartPoint.y)/2+ mStartPoint.y);
        
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mPath.reset();
        mPath.moveTo(mStartPoint.x, mStartPoint.y);
        mPath.quadTo(mControlPoint.x, mControlPoint.y, mEndPoint.x, mEndPoint.y);
        canvas.drawPath(mPath,mPaint);
        mPaint.setColor(Color.GRAY);
        canvas.drawLine(mStartPoint.x,mStartPoint.y,mControlPoint.x,mControlPoint.y,mPaint);
        canvas.drawLine(mControlPoint.x,mControlPoint.y,mEndPoint.x,mEndPoint.y,mPaint);
        /*mPaint.setColor(Color.GREEN);
        canvas.drawLine(mStartPoint.x,mStartPoint.y,mEndPoint.x,mEndPoint.y,mPaint);*/
        mPaint.setColor(Color.BLUE);
        canvas.drawPoint(mStartPoint.x,mStartPoint.y,mPaint);
        canvas.drawPoint(mControlPoint.x,mControlPoint.y,mPaint);
        canvas.drawPoint(mEndPoint.x,mEndPoint.y,mPaint);
    }
    public void startAnimation(){
        //PropertyValuesHolder xValuesHolder = PropertyValuesHolder.ofInt("x",mStartPoint.x,100,333);
        PropertyValuesHolder yValuesHolder = PropertyValuesHolder.ofInt("y",(mEndPoint.y - mStartPoint.y)/2+ mStartPoint.y,300);
        ValueAnimator valueAnimator = ValueAnimator.ofPropertyValuesHolder(yValuesHolder);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                //mControlPoint = (Point) animation.getAnimatedValue();
               // mControlPoint.x = (int) animation.getAnimatedValue("x");
                mControlPoint.y = (int) animation.getAnimatedValue("y");
                invalidate();
            }
        });
        valueAnimator.setDuration(3000);
        valueAnimator.setRepeatCount(ValueAnimator.INFINITE);
        valueAnimator.setRepeatMode(ValueAnimator.REVERSE);
        valueAnimator.start();
    }
}
